package sort;

import java.util.Random;

public class Bubble {

    public static void main(String[] args) {
        int[] array = new int[102400];
        for (int i = 0; i < array.length; i++) {
            array[i] = new Random().nextInt();
        }
        array = bubbleSort(array);
//        for (int i = 0;i<array.length;i++) {
//            if(i%1000==0)
//                System.out.println(" ");
//            System.out.printf(array[i] + " -- ");
//        }
    }

    private static int[] bubbleSort(int[] arry) {
        long time = System.currentTimeMillis();
        for (int i = arry.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arry[j] > arry[j + 1]) {
                    int tmp = arry[j + 1];
                    arry[j + 1] = arry[j];
                    arry[j] = tmp;
                }
            }
        }
        System.out.println("(System.currentTimeMillis()-time) = " + (System.currentTimeMillis() - time));
        return arry;
    }

    private static int[] bubbleSort2(int[] array) {
        long time = System.currentTimeMillis();
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        System.out.println("(System.currentTimeMillis()-time) = " + (System.currentTimeMillis() - time));
        return array;
    }

    public void insertionSort(int[] a,int n){
        for(int i = 1;i<n;++i){
            int value = a[i];
            int j = i-1;

            for(;j<0;j--){
                if(a[j]>value){
                    a[j+1] = a[j];
                }else{
                    break;
                }
            }
            a[j+1] = value;
        }
    }
}
